package web

import (
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
)

// RMController resource-manage公共资源分类管理
type RMController struct {
	RMBll *bll.ResourceManagement `inject:""`
}

// QueryPage 查询分页
func (a *RMController) QueryPage(ctx *context.Context) error {

	pageIndex, pageSize := ctx.GetPageIndex(), ctx.GetPageSize()
	params := new(schema.RMQParams)
	params.RName = ctx.Query("resource_name")
	params.ClassID = ctx.Query("class_id")
	params.Address = ctx.Query("address")
	params.Status = ctx.QueryInt("status")

	total, items, err := a.RMBll.QueryPage(params, pageIndex, pageSize)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	list := make([]map[string]interface{}, 0)
	for _, item := range items {
		tempMap := make(map[string]interface{})
		tempMap["record_id"] = item.RecordID
		tempMap["resource_name"] = item.ResourceName
		tempMap["classification_id"] = item.ClassificationID
		tempMap["address"] = item.Address
		tempMap["price"] = item.Price
		tempMap["comment"] = item.Comment
		tempMap["manager"] = item.Manager
		tempMap["telephone"] = item.Telephone
		tempMap["address_zone"] = item.AddressZone
		tempMap["available_status"] = item.AvailableStatus
		tempMap["pictures"] = item.Pictures
		tempMap["available_time"] = item.AvailableTime
		list = append(list, tempMap)
	}
	result := map[string]interface{}{
		"list": list,
		"pagination": map[string]interface{}{
			"total":    total,
			"current":  pageIndex,
			"pageSize": pageSize,
		},
	}

	return ctx.ResSuccess(result)
}

// Create 创建资源
func (a *RMController) Create(ctx *context.Context) error {
	ctx.WriteOperateLog("ResourceManagement.Create", "公共资源管理-创建资源")
	item := new(schema.ResourceManagement)
	if err := ctx.ParseJSON(item); err != nil {
		return ctx.ResErrBadRequest(err)
	}

	item.Creator = ctx.GetUserID()
	err := a.RMBll.Create(item)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Get 查询资源
func (a *RMController) Get(ctx *context.Context) error {
	item, err := a.RMBll.Get(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	result := make(map[string]interface{})
	result["record_id"] = item.RecordID
	result["resource_name"] = item.ResourceName
	result["classification_id"] = item.ClassificationID
	result["address"] = item.Address
	result["price"] = item.Price
	result["comment"] = item.Comment
	result["manager"] = item.Manager
	result["telephone"] = item.Telephone
	result["address_zone"] = item.AddressZone
	result["available_status"] = item.AvailableStatus
	result["pictures"] = item.Pictures
	result["available_time"] = item.AvailableTime
	return ctx.ResSuccess(result)
}

// Update 更新资源
func (a *RMController) Update(ctx *context.Context) error {
	ctx.WriteOperateLog("ResourceManagement.Update", "公共资源管理-更新数据")
	item := new(schema.ResourceManagement)
	if err := ctx.ParseJSON(item); err != nil {
		return ctx.ResErrBadRequest(err)
	}

	err := a.RMBll.Update(ctx.Param("id"), item)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Delete 标记删除资源
func (a *RMController) Delete(ctx *context.Context) error {
	ctx.WriteOperateLog("ResourceManagement.Delete", "公共资源管理-删除数据")
	id := ctx.Param("id")

	err := a.RMBll.Delete(id)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// QueryCategory 查询公共资源的所有的所属分类
func (a *RMController) QueryCategory(ctx *context.Context) error {
	items, err := a.RMBll.QueryCategory()
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(items)
}

// QueryParksZones 获取公共资源相关的所有的园区和区域
func (a *RMController) QueryParksZones(ctx *context.Context) error {
	items, err := a.RMBll.QueryParksZones()
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(items)
}
